<?php

namespace app\controller\admin\wz;

use app\model\admin\wz\Wzhero;
use app\model\admin\wz\WzheroMatch;
use http\Client;
use laytp\controller\Backend;
use laytp\library\CommonFun;

/**
 * Class Game
 * @package app\controller\admin\run
 */
class Hero extends Backend
{
    /**
     * api_log模型对象
     * @var \app\model\api\Log
     */
    protected $model;
    protected $hasSoftDel = 0;//是否拥有软删除功能

    protected $noNeedLogin = []; // 无需登录即可请求的方法
    protected $noNeedAuth = ['index', 'info']; // 无需鉴权即可请求的方法

    public $orderRule = ['id' => 'asc'];

    public function _initialize()
    {
        $this->model = new Wzhero();
    }

    /**
     * 列表
     *  all_data参数的值为true时，表示查询表中所有数据集，否则进行分页查询
     * @return mixed
     */
    public function index()
    {
        $postData = $this->buildPostParams();
        $data = $this->postData("getHeroList", $postData);
        $responseData = $data['data'];
        $responseData['data'] = $responseData['list'];
        unset($responseData['list']);
        return $this->success('数据获取成功', $responseData);
    }


    /**
     * 列表
     *  all_data参数的值为true时，表示查询表中所有数据集，否则进行分页查询
     * @return mixed
     */
    public function list()
    {
        $where = $this->buildSearchParams();
        $order = $this->buildOrder();
        $data = $this->model->where($where)->order($order)->cache(true)->select()->toArray();
        return $this->success('数据获取成功', $data);
    }


    //查看详情
    public function info()
    {
        $id = $this->request->param('id');
        $info = $this->postData("readHero", ['id' => $id]);
        $info = $info['data'];
        $info['position'] = strtr($info['position'], '/', ',');
        $info['survival_ability'] = $info['survivalAbility'];
        $info['hero_id'] = $info['heroId'];
        $info['short_name'] = $info['shortName'];
        $info['dmg_ability'] = $info['dmgAbility'];
        $info['skill_eff'] = $info['skillEff'];
        $info['difficulty'] = $info['difficulty'];
        $info['url'] = imgUrlToAbsolute($info['url']);
        $info['icon'] = imgUrlToAbsolute($info['icon']);
        $info['relation_hero_id1'] = $info['relationsList'][0]['relationHeroId'];
        $info['remark1'] = !empty($info['relationsList'][0]['remark']) ? $info['relationsList'][0]['remark'] : '';
        $info['relation_hero_id2'] = $info['relationsList'][1]['relationHeroId'];
        $info['remark2'] = !empty($info['relationsList'][1]['remark']) ? $info['relationsList'][1]['remark'] : '';
        $info['relation_hero_id3'] = $info['relationsList'][2]['relationHeroId'];
        $info['remark3'] = !empty($info['relationsList'][2]['remark']) ? $info['relationsList'][2]['remark'] : '';
        $info['relation_hero_id4'] = $info['relationsList'][3]['relationHeroId'];
        $info['remark4'] = !empty($info['relationsList'][3]['remark']) ? $info['relationsList'][3]['remark'] : '';
        $info['relation_hero_id5'] = $info['relationsList'][4]['relationHeroId'];
        $info['remark5'] = !empty($info['relationsList'][4]['remark']) ? $info['relationsList'][4]['remark'] : '';
        $info['relation_hero_id6'] = $info['relationsList'][5]['relationHeroId'];
        $info['remark6'] = !empty($info['relationsList'][5]['remark']) ? $info['relationsList'][5]['remark'] : '';
        for ($i = 1; $i <= 10; $i++) {
            $info['skin_name' . $i] = !empty($info['skinsList'][$i - 1]['name']) ? $info['skinsList'][$i - 1]['name'] : '';
            $info['skin_url' . $i] = !empty($info['skinsList'][$i - 1]['url']) ? imgUrlToAbsolute($info['skinsList'][$i - 1]['url']) : '';
        }
        $flag1 = 4;
        $flag2 = 1;
        foreach ($info['teamsList'] as $index => $item) {
            if (!empty($item['relationHeroTwo'])) {
                if ($flag1 == 4) {
                    $info['relation_hero_one4'] = !empty($item['relationHeroOne']) ? $item['relationHeroOne'] : '';
                    $info['relation_hero_two4'] = !empty($item['relationHeroTwo']) ? $item['relationHeroTwo'] : '';
                    $info['combo4'] = !empty($item['combo']) ? $item['combo'] : '';
                    $info['fit4'] = !empty($item['fit']) ? $item['fit'] : '';
                    $flag1++;
                } elseif ($flag1 == 5) {
                    $info['relation_hero_one5'] = !empty($item['relationHeroOne']) ? $item['relationHeroOne'] : '';
                    $info['relation_hero_two5'] = !empty($item['relationHeroTwo']) ? $item['relationHeroTwo'] : '';
                    $info['combo5'] = !empty($item['combo']) ? $item['combo'] : '';
                    $info['fit5'] = !empty($item['fit']) ? $item['fit'] : '';
                    $flag1++;
                } elseif ($flag1 == 6) {
                    $info['relation_hero_one6'] = !empty($item['relationHeroOne']) ? $item['relationHeroOne'] : '';
                    $info['relation_hero_two6'] = !empty($item['relationHeroTwo']) ? $item['relationHeroTwo'] : '';
                    $info['combo6'] = !empty($item['combo']) ? $item['combo'] : '';
                    $info['fit6'] = !empty($item['fit']) ? $item['fit'] : '';
                    $flag1++;
                }
            } else {
                if ($flag2 == 1) {
                    $info['relation_hero_one1'] = !empty($item['relationHeroOne']) ? $item['relationHeroOne'] : '';
                    $info['relation_hero_two1'] = !empty($item['relationHeroTwo']) ? $item['relationHeroTwo'] : '';
                    $info['combo1'] = !empty($item['combo']) ? $item['combo'] : '';
                    $info['fit1'] = !empty($item['fit']) ? $item['fit'] : '';
                    $flag2++;
                } elseif ($flag2 == 2) {
                    $info['relation_hero_one2'] = !empty($item['relationHeroOne']) ? $item['relationHeroOne'] : '';
                    $info['relation_hero_two2'] = !empty($item['relationHeroTwo']) ? $item['relationHeroTwo'] : '';
                    $info['combo2'] = !empty($item['combo']) ? $item['combo'] : '';
                    $info['fit2'] = !empty($item['fit']) ? $item['fit'] : '';
                    $flag2++;
                } elseif ($flag2 == 3) {
                    $info['relation_hero_one3'] = !empty($item['relationHeroOne']) ? $item['relationHeroOne'] : '';
                    $info['relation_hero_two3'] = !empty($item['relationHeroTwo']) ? $item['relationHeroTwo'] : '';
                    $info['combo3'] = !empty($item['combo']) ? $item['combo'] : '';
                    $info['fit3'] = !empty($item['fit']) ? $item['fit'] : '';
                    $flag2++;
                }
            }
        }
        if (isset($info->icon)) {
            $info->icon = imgUrlToAbsolute($info->icon);
        }
        if (isset($info->picture)) {
            $info->picture = imgUrlToAbsolute($info->picture);
        }
        return $this->success('获取成功', $info);
    }

    //添加
    public function add()
    {
        $post = CommonFun::filterPostData($this->request->post());
//        $post['url'] = parse_url($post['url']);
//        $post['skin_url1'] = parse_url($post['skin_url1']);
//        $post['skin_url2'] = parse_url($post['skin_url2']);
//        $post['skin_url3'] = parse_url($post['skin_url3']);
//        $post['skin_url4'] = parse_url($post['skin_url4']);
//        $post['skin_url5'] = parse_url($post['skin_url5']);
//        $post['skin_url6'] = parse_url($post['skin_url6']);
//        $post['skin_url7'] = parse_url($post['skin_url7']);
//        $post['skin_url8'] = parse_url($post['skin_url8']);
//        $post['skin_url9'] = parse_url($post['skin_url9']);
//        $post['skin_url10'] = parse_url($post['skin_url10']);
        $skinList = [];
        if (!empty($post['skin_name1'])) {
            array_push($skinList, array('name' => $post['skin_name1'], 'url' => $post['skin_url1']));
        }
        if (!empty($post['skin_name2'])) {
            array_push($skinList, array('name' => $post['skin_name2'], 'url' => $post['skin_url2']));
        }
        if (!empty($post['skin_name3'])) {
            array_push($skinList, array('name' => $post['skin_name3'], 'url' => $post['skin_url3']));
        }
        if (!empty($post['skin_name4'])) {
            array_push($skinList, array('name' => $post['skin_name4'], 'url' => $post['skin_url4']));
        }
        if (!empty($post['skin_name5'])) {
            array_push($skinList, array('name' => $post['skin_name5'], 'url' => $post['skin_url5']));
        }
        if (!empty($post['skin_name6'])) {
            array_push($skinList, array('name' => $post['skin_name6'], 'url' => $post['skin_url6']));
        }
        if (!empty($post['skin_name7'])) {
            array_push($skinList, array('name' => $post['skin_name7'], 'url' => $post['skin_url7']));
        }
        if (!empty($post['skin_name8'])) {
            array_push($skinList, array('name' => $post['skin_name8'], 'url' => $post['skin_url8']));
        }
        if (!empty($post['skin_name9'])) {
            array_push($skinList, array('name' => $post['skin_name9'], 'url' => $post['skin_url9']));
        }
        if (!empty($post['skin_name10'])) {
            array_push($skinList, array('name' => $post['skin_name10'], 'url' => $post['skin_url10']));
        }
        $relation_list = [];
        if (!empty($post['relation_hero_id1'])) {
            array_push($relation_list, array('type' => 1, 'relation_hero_id' => $post['relation_hero_id1'], 'remark' => !empty($post['remark1']) ? $post['remark1'] : ''));
        }
        if (!empty($post['relation_hero_id2'])) {
            array_push($relation_list, array('type' => 1, 'relation_hero_id' => $post['relation_hero_id2'], 'remark' => !empty($post['remark2']) ? $post['remark2'] : ''));
        }
        if (!empty($post['relation_hero_id3'])) {
            array_push($relation_list, array('type' => 1, 'relation_hero_id' => $post['relation_hero_id3'], 'remark' => !empty($post['remark3']) ? $post['remark3'] : ''));
        }
        if (!empty($post['relation_hero_id4'])) {
            array_push($relation_list, array('type' => 2, 'relation_hero_id' => $post['relation_hero_id4'], 'remark' => !empty($post['remark4']) ? $post['remark4'] : ''));
        }
        if (!empty($post['relation_hero_id5'])) {
            array_push($relation_list, array('type' => 2, 'relation_hero_id' => $post['relation_hero_id5'], 'remark' => !empty($post['remark5']) ? $post['remark5'] : ''));
        }
        if (!empty($post['relation_hero_id6'])) {
            array_push($relation_list, array('type' => 2, 'relation_hero_id' => $post['relation_hero_id6'], 'remark' => !empty($post['remark6']) ? $post['remark6'] : ''));
        }

        $team_list = [];
        if (!empty($post['relation_hero_one1'])) {
            array_push($team_list, array('type' => 1, 'relation_hero_one' => $post['relation_hero_one1'], 'relation_hero_two' => '0', 'fit' => !empty($post['fit1']) ? $post['fit1'] : '', 'combo' => !empty($post['combo1']) ? $post['combo1'] : '', 'remark' => !empty($post['remark1'])? $post['remark1'] : ''));
        }
        if (!empty($post['relation_hero_one2'])) {
            array_push($team_list, array('type' => 1, 'relation_hero_one' => $post['relation_hero_one2'], 'relation_hero_two' => '0', 'fit' => !empty($post['fit2']) ? $post['fit2'] : '', 'combo' => !empty($post['combo2']) ? $post['combo2'] : '', 'remark' => !empty($post['remark2']) ? $post['remark2'] : ''));
        }
        if (!empty($post['relation_hero_one3'])) {
            array_push($team_list, array('type' => 1, 'relation_hero_one' => $post['relation_hero_one3'], 'relation_hero_two' => '0', 'fit' => !empty($post['fit3']) ? $post['fit3'] : '', 'combo' => !empty($post['combo3']) ? $post['combo3'] : '', 'remark' => !empty($post['remark3']) ? $post['remark3'] : ''));
        }

        if (!empty($post['relation_hero_one4'])) {
            array_push($team_list, array('type' => 2, 'relation_hero_one' => $post['relation_hero_one4'], 'relation_hero_two' => $post['relation_hero_two4'], 'fit' => !empty($post['fit4']) ? $post['fit4'] : '', 'combo' => !empty($post['combo4']) ? $post['combo4'] : '', 'remark' => !empty($post['remark4']) ? $post['remark4'] : ''));
        }
        if (!empty($post['relation_hero_one5'])) {
            array_push($team_list, array('type' => 2, 'relation_hero_one' => $post['relation_hero_one5'], 'relation_hero_two' => $post['relation_hero_two5'], 'fit' => !empty($post['fit5']) ? $post['fit5'] : '', 'combo' => !empty($post['combo5']) ? $post['combo5'] : '', 'remark' => !empty($post['remark5']) ? $post['remark5'] : ''));
        }
        if (!empty($post['relation_hero_one6'])) {
            array_push($team_list, array('type' => 2, 'relation_hero_one' => $post['relation_hero_one6'], 'relation_hero_two' => $post['relation_hero_two6'], 'fit' => !empty($post['fit6']) ? $post['fit6'] : '', 'combo' => !empty($post['combo6']) ? $post['combo6'] : '', 'remark' => !empty($post['remark6']) ? $post['remark6'] : ''));
        }

        $data = array(
            'hero_id' => $post['hero_id'],
            'name' => $post['name'],
            'short_name' => $post['short_name'],
            'nickname' => $post['nickname'],
            'icon' => $post['icon'],
            'url' => $post['url'],
            'type' => $post['type'],
            'position' => strtr($post['position'], ',', '/'),
            'survival_ability' => $post['survival_ability'],
            'survival_ability_label' => '生存能力',
            'dmg_ability' => $post['dmg_ability'],
            'dmg_ability_label' => '伤害能力',
            'skill_eff' => $post['skill_eff'],
            'skill_eff_label' => '技能效果',
            'difficulty' => $post['difficulty'],
            'difficulty_label' => '上手难度',
            'relation_list' => json_encode($relation_list, JSON_UNESCAPED_UNICODE),
            'team_list' => json_encode($team_list, JSON_UNESCAPED_UNICODE),
            'skins_list' => json_encode($skinList, JSON_UNESCAPED_UNICODE)
        );


        try {
            $hero = $this->postData("createHero", $data);
            if ($hero) {
                $this->clearCache();
                return $this->success('添加成功', $post);
            } else {
                return $this->error('操作失败');
            }
        } catch (\Exception $e) {
            return $this->exceptionError($e);
        }
    }

    //编辑
    public function edit()
    {
        $id = $this->request->param('id');
        $info = $this->postData("readHero", ['id' => $id]);
        if (!$info) {
            return $this->error('ID参数错误');
        }
        $post = CommonFun::filterPostData($this->request->post());
//        $post['url'] = parse_url($post['url']);
//        $post['skin_url1'] = parse_url($post['skin_url1']);
//        $post['skin_url2'] = parse_url($post['skin_url2']);
//        $post['skin_url3'] = parse_url($post['skin_url3']);
//        $post['skin_url4'] = parse_url($post['skin_url4']);
//        $post['skin_url5'] = parse_url($post['skin_url5']);
//        $post['skin_url6'] = parse_url($post['skin_url6']);
//        $post['skin_url7'] = parse_url($post['skin_url7']);
//        $post['skin_url8'] = parse_url($post['skin_url8']);
//        $post['skin_url9'] = parse_url($post['skin_url9']);
//        $post['skin_url10'] = parse_url($post['skin_url10']);
        $skinList = [];
        if (!empty($post['skin_name1'])) {
            array_push($skinList, array('name' => $post['skin_name1'], 'url' => $post['skin_url1']));
        }
        if (!empty($post['skin_name2'])) {
            array_push($skinList, array('name' => $post['skin_name2'], 'url' => $post['skin_url2']));
        }
        if (!empty($post['skin_name3'])) {
            array_push($skinList, array('name' => $post['skin_name3'], 'url' => $post['skin_url3']));
        }
        if (!empty($post['skin_name4'])) {
            array_push($skinList, array('name' => $post['skin_name4'], 'url' => $post['skin_url4']));
        }
        if (!empty($post['skin_name5'])) {
            array_push($skinList, array('name' => $post['skin_name5'], 'url' => $post['skin_url5']));
        }
        if (!empty($post['skin_name6'])) {
            array_push($skinList, array('name' => $post['skin_name6'], 'url' => $post['skin_url6']));
        }
        if (!empty($post['skin_name7'])) {
            array_push($skinList, array('name' => $post['skin_name7'], 'url' => $post['skin_url7']));
        }
        if (!empty($post['skin_name8'])) {
            array_push($skinList, array('name' => $post['skin_name8'], 'url' => $post['skin_url8']));
        }
        if (!empty($post['skin_name9'])) {
            array_push($skinList, array('name' => $post['skin_name9'], 'url' => $post['skin_url9']));
        }
        if (!empty($post['skin_name10'])) {
            array_push($skinList, array('name' => $post['skin_name10'], 'url' => $post['skin_url10']));
        }

        $relation_list = [];
        if (!empty($post['relation_hero_id1'])) {
            array_push($relation_list, array('type' => 1, 'relation_hero_id' => $post['relation_hero_id1'], 'remark' => !empty($post['remark1']) ? $post['remark1'] : ''));
        }
        if (!empty($post['relation_hero_id2'])) {
            array_push($relation_list, array('type' => 1, 'relation_hero_id' => $post['relation_hero_id2'], 'remark' => !empty($post['remark2']) ? $post['remark2'] : ''));
        }
        if (!empty($post['relation_hero_id3'])) {
            array_push($relation_list, array('type' => 1, 'relation_hero_id' => $post['relation_hero_id3'], 'remark' => !empty($post['remark3']) ? $post['remark3'] : ''));
        }
        if (!empty($post['relation_hero_id4'])) {
            array_push($relation_list, array('type' => 2, 'relation_hero_id' => $post['relation_hero_id4'], 'remark' => !empty($post['remark4']) ? $post['remark4'] : ''));
        }
        if (!empty($post['relation_hero_id5'])) {
            array_push($relation_list, array('type' => 2, 'relation_hero_id' => $post['relation_hero_id5'], 'remark' => !empty($post['remark5']) ? $post['remark5'] : ''));
        }
        if (!empty($post['relation_hero_id6'])) {
            array_push($relation_list, array('type' => 2, 'relation_hero_id' => $post['relation_hero_id6'], 'remark' => !empty($post['remark6']) ? $post['remark6'] : ''));
        }

        $team_list = [];
        if (!empty($post['relation_hero_one1'])) {
            array_push($team_list, array('type' => 1, 'relation_hero_one' => $post['relation_hero_one1'], 'relation_hero_two' => '0', 'fit' => !empty($post['fit1']) ? $post['fit1'] : '', 'combo' => !empty($post['combo1']) ? $post['combo1'] : '', 'remark' => !empty($post['remark1'])? $post['remark1'] : ''));
        }
        if (!empty($post['relation_hero_one2'])) {
            array_push($team_list, array('type' => 1, 'relation_hero_one' => $post['relation_hero_one2'], 'relation_hero_two' => '0', 'fit' => !empty($post['fit2']) ? $post['fit2'] : '', 'combo' => !empty($post['combo2']) ? $post['combo2'] : '', 'remark' => !empty($post['remark2']) ? $post['remark2'] : ''));
        }
        if (!empty($post['relation_hero_one3'])) {
            array_push($team_list, array('type' => 1, 'relation_hero_one' => $post['relation_hero_one3'], 'relation_hero_two' => '0', 'fit' => !empty($post['fit3']) ? $post['fit3'] : '', 'combo' => !empty($post['combo3']) ? $post['combo3'] : '', 'remark' => !empty($post['remark3']) ? $post['remark3'] : ''));
        }

        if (!empty($post['relation_hero_one4'])) {
            array_push($team_list, array('type' => 2, 'relation_hero_one' => $post['relation_hero_one4'], 'relation_hero_two' => $post['relation_hero_two4'], 'fit' => !empty($post['fit4']) ? $post['fit4'] : '', 'combo' => !empty($post['combo4']) ? $post['combo4'] : '', 'remark' => !empty($post['remark4']) ? $post['remark4'] : ''));
        }
        if (!empty($post['relation_hero_one5'])) {
            array_push($team_list, array('type' => 2, 'relation_hero_one' => $post['relation_hero_one5'], 'relation_hero_two' => $post['relation_hero_two5'], 'fit' => !empty($post['fit5']) ? $post['fit5'] : '', 'combo' => !empty($post['combo5']) ? $post['combo5'] : '', 'remark' => !empty($post['remark5']) ? $post['remark5'] : ''));
        }
        if (!empty($post['relation_hero_one6'])) {
            array_push($team_list, array('type' => 2, 'relation_hero_one' => $post['relation_hero_one6'], 'relation_hero_two' => $post['relation_hero_two6'], 'fit' => !empty($post['fit6']) ? $post['fit6'] : '', 'combo' => !empty($post['combo6']) ? $post['combo6'] : '', 'remark' => !empty($post['remark6']) ? $post['remark6'] : ''));
        }
        $data = array(
            'id' => $post['id'],
            'hero_id' => $post['hero_id'],
            'name' => $post['name'],
            'short_name' => $post['short_name'],
            'nickname' => $post['nickname'],
            'icon' => $post['icon'],
            'url' => $post['url'],
            'type' => $post['type'],
            'position' => strtr($post['position'], ',', '/'),
            'survival_ability' => $post['survival_ability'],
            'survival_ability_label' => '生存能力',
            'dmg_ability' => $post['dmg_ability'],
            'dmg_ability_label' => '伤害能力',
            'skill_eff' => $post['skill_eff'],
            'skill_eff_label' => '技能效果',
            'difficulty' => $post['difficulty'],
            'difficulty_label' => '上手难度',
            'relation_list' => json_encode($relation_list, JSON_UNESCAPED_UNICODE),
            'team_list' => json_encode($team_list, JSON_UNESCAPED_UNICODE),
            'skins_list' => json_encode($skinList, JSON_UNESCAPED_UNICODE)
        );
        try {
            $response = $this->postData("updateHero", $data);
            if ($response['code'] == 0) {
                $this->clearCache();
                return $this->success('添加成功', $post);
            } else {
                return $this->error($response['message']);
            }
        } catch (\Exception $e) {
            return $this->exceptionError($e);
        }
    }

    //删除
    public function del()
    {
        $ids = array_filter($this->request->post('ids'));
        if (!$ids) {
            return $this->error('参数ids不能为空');
        }
        try {
            $response = $this->postData("deleteHero", [
                'id' => $ids[0],
            ]);
            if ($response['code'] == 0) {
                $this->clearCache();
                return $this->success('删除成功');
            } else {
                return $this->error($response['message']);
            }
        } catch (\Exception $e) {
            return $this->exceptionError($e);
        }
    }

    //设置展示
    public function setIsShow()
    {
        $id       = $this->request->post('id');
        $fieldVal = $this->request->post('field_val');
        $isRecycle = $this->request->post('is_recycle');
        $update['is_show'] = $fieldVal;

        try {
            if($isRecycle) {
                $updateRes = $this->model->onlyTrashed()->where('id', '=', $id)->update($update);
            } else {
                $updateRes = $this->model->where('id', '=', $id)->update($update);
            }

            if ($updateRes) {
                $this->clearCache();
                return $this->success('操作成功');
            } else if ($updateRes === 0) {
                return $this->success('未作修改');
            } else {
                return $this->error('操作失败');
            }
        } catch (\Exception $e) {
            return $this->exceptionError($e);
        }
    }


    //设置排序
    public function setSort()
    {
        $id       = $this->request->post('id');
        $fieldVal = $this->request->post('field_val');
        $isRecycle = $this->request->post('is_recycle');
        $update['list_sort'] = $fieldVal;

        try {
            if($isRecycle) {
                $updateRes = $this->model->onlyTrashed()->where('id', '=', $id)->update($update);
            } else {
                $updateRes = $this->model->where('id', '=', $id)->update($update);
            }

            if ($updateRes) {
                $this->clearCache();
                return $this->success('操作成功');
            } else if ($updateRes === 0) {
                return $this->success('未作修改');
            } else {
                return $this->error('操作失败');
            }
        } catch (\Exception $e) {
            return $this->exceptionError($e);
        }
    }

    public function match(){
        return $this->success('操作成功',(new WzheroMatch())->select());
    }
}